<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" @click="handleCreate">
        注册
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户账号">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="描述">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleRole(row)">
            角色分配
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <div v-if="dialogStatus==='create'">
          <el-form-item label="账号" prop="username">
            <el-input v-model="temp.username" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="temp.password" />
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="temp.name" />
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="temp.content" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请填写描述" />
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="角色">
            <el-radio-group v-model="roleType">
              <el-radio :label="1">角色1</el-radio>
              <el-radio :label="2">角色2</el-radio>
              <el-radio :label="3">角色3</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateRole()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      temp: {
        id: undefined,
        username: '',
        name: '',
        password: '',
        content: '', 
        role: undefined
      },
      roleType: undefined,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        create: '添加',
        setrole: '角色分配'
      },
      list: [
        {id: 1, username: 'user1', name:'用户1', role: undefined, password: '123456', content: '用户描述，用户描述'},
        {id: 2, username: 'user1', name:'用户1', role: undefined, password:'用户2', content: '用户描述，用户描述'},
        {id: 3, username: 'user1', name:'用户1', role: undefined, password:'用户3', content: '用户描述，用户描述'},
      ],
      rules: {
        username: [{ required: true, message: '请填写用户名称', trigger: 'change' }],
        password: [{ required: true, message: '请填写密码', trigger: 'blur' }],
      },
      listLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    //角色分配
    handleRole(row) {
      this.temp = Object.assign({}, row); // copy obj
      this.roleType = this.temp.role;
      this.dialogStatus = 'setrole';
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    resetTemp() {
      this.temp = {
        id: undefined,
        username: '',
        name: '',
        password: '',
        content: '', 
        role: undefined
      }
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          // createArticle(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
          // })
        }
      })
    },
    updateRole() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.role = this.roleType;
          const tempData = Object.assign({}, this.temp)
          // updateArticle(tempData).then(() => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          // })
        }
      })
    },
    fetchData() {
      this.listLoading = false
    }
  }
}
</script>
